﻿using System;

namespace WorkFlow.DBProxy
{
    /// <summary>
    /// 该类对html的id进行简单加密
    /// </summary>
    public class SecHtmlID
    {
        public static string DecodeID(string unDecodeId)
        {
            if (string.IsNullOrEmpty(unDecodeId)) return string.Empty;

            char[] chs = unDecodeId.Trim().ToCharArray();
            int idlength = chs.Length / 2;
            char[] cId = new char[idlength];
            for (int i = 0; i < idlength; i++)
            {
                cId[i] = (char)((int)chs[2 * i]);
            }

            return new string(cId);
        }

        public static string EncriptID(string unEncriptedId)
        {
            if (string.IsNullOrEmpty(unEncriptedId)) return string.Empty;

            string id = unEncriptedId.Trim();
            char[] chs = unEncriptedId.ToCharArray();
            int count = chs.Length;
            int max = (int)chs[0], min = (int)chs[0];
            for (int i = 0; i < count; i++)
            {
                if ((int)chs[i] > max) max = (int)chs[i];
                if ((int)chs[i] < min) min = (int)chs[i];
            }

            Random rd = new Random();
            char[] temp = new char[2 * count];
            int tempInt = 111;
            for (int i = 0; i < count; i++)
            {
                temp[2 * i] = (char)((int)chs[i]);
                tempInt = rd.Next(min, max);
                if (tempInt >= 58 && tempInt <= 64) tempInt += 12;
                if (tempInt >= 91 && tempInt <= 96) tempInt += rd.Next(10, 20);
                temp[2 * i + 1] = (char)(tempInt);
            }

            return new string(temp);
        }
    }
}